package com.hk.productservice.service.impl;

import com.hk.model.ProductMenu;
import com.hk.productservice.mapper.ProductMenuMapper;
import com.hk.productservice.service.ProductMenuService;
import com.hk.utlis.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zhe
 * @Date: 2021/6/18 17:02
 * @Statement: 产品菜单Service层接口实现类
 */
@Service
public class ProductMenuServiceImpl implements ProductMenuService {

    @Autowired
    ProductMenuMapper iProductMenuMapper;

    @Override
    public List<ProductMenu> findAll(Integer page, Integer limit, ProductMenu t) {
//        PageHelper.startPage(page,limit);
        List<ProductMenu> productMenuList = iProductMenuMapper.findAll(t);
//        处理新闻类型
        if (productMenuList.isEmpty()) {
            System.out.println("暂时没有新闻类型！");
            productMenuList = new ArrayList<>();
            return productMenuList;
        }

//        存储父节点新闻类型菜单
        List<ProductMenu> productMenus = new ArrayList<>();
//        0:根节点
        for (ProductMenu productMenu : productMenuList) {
            if (productMenu.getParentId() == 0) {
                productMenus.add(productMenu);
            }
        }
//        判断是否具有子节点，没有就直接终止返回查询的原始数据
        if (productMenus.isEmpty()) {
            return productMenuList;
        }

//        查询子节点
        for (ProductMenu productMenu : productMenus) {
            //        存储子级类型菜单
            List<ProductMenu> productMenuList1 = getChild(productMenu.getId(),productMenuList);
            productMenu.setProductMenus(productMenuList1);
        }
        return productMenus;
    }


    /**
     * 递归查询子节点
     * @param id 父级Id
     * @param productMenuList
     * @return
     */
    public List<ProductMenu> getChild(int id,List<ProductMenu> productMenuList) {
        List<ProductMenu> childList = new ArrayList<>();
        //      匹配父节点
        for (ProductMenu productMenu : productMenuList) {
//            匹配成功
            if (id == productMenu.getParentId()) {
                childList.add(productMenu);
            }
        }

        for (ProductMenu productMenu : childList) {
            List<ProductMenu> menuList = getChild(productMenu.getId(), productMenuList);
            productMenu.setProductMenus(menuList);
        }
//        当前节点没有子节点，退出
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

    @Override
    public int count(Integer page, Integer limit, ProductMenu t) {
//        PageHelper.startPage(page,limit);
        int count = iProductMenuMapper.count(t);
        return count;
    }

    @Override
    public ProductMenu findById(Integer id) {
        ProductMenu productMenu = iProductMenuMapper.findById(id);
        return productMenu;
    }

    /**
     * 递归查找当前Id对应的所属所有子节点ID
     * @param ids
     * @return
     */
    @Override
    public List<ProductMenu> findByParentId(List<Integer> ids) {
//         用来存储查询的值
        List<ProductMenu> productMenuList1 = new ArrayList<>();
        List<ProductMenu> productMenuList = iProductMenuMapper.findByParentId(ids);
        List<Integer> idList = productMenuList.stream().map(ProductMenu::getId).collect(Collectors.toList());
//        递归匹配 2参数为匹配 1参数主要为赋值
        productMenuList1 = getfindByParentId(productMenuList1,productMenuList,idList);
        return productMenuList1;
    }

    /**
     * 递归查询所属子节点
     * @return
     */
    public List<ProductMenu> getfindByParentId(List<ProductMenu> productMenuList,List<ProductMenu> menuList,List<Integer> ids) {
//        判断不为空 赋值
        if (!menuList.isEmpty()) {
            for (ProductMenu menu : menuList) {
                productMenuList.add(menu);
            }
            List<ProductMenu> productMenuList1 = iProductMenuMapper.findByParentId(ids);
            List<Integer> idList = productMenuList1.stream().map(ProductMenu::getId).collect(Collectors.toList());
            getfindByParentId(productMenuList,productMenuList1,idList);
        }

        /**
         * 退出
         */
        if (menuList.isEmpty()) {
            return new ArrayList<ProductMenu>();
        }
        return productMenuList;

    }

    @Override
    public int save(ProductMenu t) {
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtCreate(dateTimeString);
        t.setGmtModified(dateTimeString);
        int save = iProductMenuMapper.save(t);
        if (save == 1) {
            System.out.println("产品类型添加成功！");
        }
        return save;
    }

    @Override
    public int update(ProductMenu t) {
        //        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtModified(dateTimeString);
        int update = iProductMenuMapper.update(t);
        if (update == 1) {
            System.out.println("产品类型修改成功");
        }
        return update;
    }

    @Override
    public int updateParentId(ProductMenu t) {
        int updateParentId = 0;
//        判断修改序号是否与自身Id匹配或是否为负数
        if (t.getParentId() == t.getId() || t.getParentId() < 0) {
            return updateParentId;
        }
//       获取所有新闻类型
        List<ProductMenu> productMenuList = iProductMenuMapper.findAll(t);
        List<Integer> ids = productMenuList.stream().map(ProductMenu::getId).collect(Collectors.toList());
//        判断所修改的序号是否存在
        for (Integer id : ids) {
            if (t.getParentId() == id) {
                // 修改日期时间
                String dateTimeString = DateUtils.dateTimeString(new Date());
                t.setGmtModified(dateTimeString);
                updateParentId = iProductMenuMapper.updateParentId(t);
                if (updateParentId == 1) {
                    System.out.println("排序修改成功");
                }
                break;
            }
        }
        return updateParentId;
    }

    @Override
    public int delete(Integer id) {
        int delete = iProductMenuMapper.delete(id);
        if (delete == 1) {
            System.out.println("产品类型删除成功");
        }
        return delete;
    }

    @Override
    public int deleteIds(List<Integer> ids) {
        int delete = iProductMenuMapper.deleteIds(ids);
        if (delete > 1) {
            System.out.println("多级产品类型删除成功");
        }
        return delete;
    }

}
